﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Edit_F4CheckRR732 : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        public string serial;

        private DataSet ds;
        private DataViewManager dsView;
        
        private Dictionary<string, string> required_list;

        public frm_Edit_F4CheckRR732()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.serial = null;
            this.ds = new DataSet("RobotOrderDetail");

            this.required_list = new Dictionary<string, string>() { { "serialNo", "Serial No" }, { "writtenBy", "Written By" },
            { "checkBy", "Check By"}, {"inputDate", "Date"}, {"wiringDiagram", "Wiring Diagram"}, {"PC_Power_Serial", "PC Power Serial"},
            {"Upper_RC420", "Upper RC420"}, {"Upper_RC420Ver", "Upper RC420 Ver"}, {"Upper_RC420No", "Upper RC420 No"},
            {"Upper_RD026", "Upper RD026"}, {"Upper_Run", "Upper Run"}, {"Upper_Stop", "Upper Stop"},
            {"Lower_RC420", "Lower RC420"}, {"Lower_RC420Ver", "Lower RC420 Ver"}, {"Lower_RC420No", "Lower RC420 No"},
            {"Lower_RD026", "Lower RD026"}, {"Lower_Run", "Lower Run"}, {"Lower_Stop", "Lower Stop"},
            {"A_RC420", "A RC420"}, {"A_RC420Ver", "A RC420 Ver"}, {"A_RC420No", "A RC420 No"},
            {"A_RD026", "A RD026"}, {"A_Run", "A Run"}, {"A_Stop", "A Stop"},
            {"Bar_RC420", "Bar RC420"}, {"Bar_RC420Ver", "Bar RC420 Ver"}, {"Bar_RC420No", "Bar RC420 No"},
            {"Bar_RD026", "Bar RD026"}, {"Bar_Run", "Bar Run"}, {"Bar_Stop", "Bar Stop"},
            {"Z_RC420", "Z RC420"}, {"Z_RC420Ver", "Z RC420 Ver"}, {"Z_RC420No", "Z RC420 No"},
            {"Z_RD026", "Z RD026"}, {"Z_Run", "Z Run"}, {"Z_Stop", "Z Stop"},
            {"RC431", "RC431"}, {"RC431Ver", "RC431 Ver"}, {"RC431No", "RC431 No"}};

            LoadSerialList();
            LoadStaffList();
            LoadDefaultData();            
        }

        ////////////////////////////////////////////////////
        /// Load Serial List
        ////////////////////////////////////////////////////
        private void LoadSerialList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo], [CompID] FROM [231_RobotOrderDetail]"
                    + " WHERE(FinishedDate > DateAdd(dd, 0, DateDiff(dd, 0, GETDATE())) - 500) AND [SerialNo] NOT IN"
                    + " (SELECT [SerialNo] COLLATE Vietnamese_CI_AS FROM [231_UnitRR732] WHERE [SerialNo] <> '" + this.serial + "')";                                                
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "RobotOrderDetail");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.serialNo.DataSource = dsView;
                this.serialNo.DisplayMember = "RobotOrderDetail.SerialNo";
                this.serialNo.ValueMember = "RobotOrderDetail.SerialNo";
                this.serialNo.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.partNo.DataBindings.Add("Text", dsView, "RobotOrderDetail.CompID");
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Load Staff List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadStaffList()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT StaffID, StaffName FROM [222_Staff] WHERE StopDate IS NULL ORDER BY StaffID";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    string[] fullname = dataReader["StaffName"].ToString().Trim().Split(' ');

                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                    TextInfo textInfo = cultureInfo.TextInfo;
                    string txt = textInfo.ToTitleCase(fullname[fullname.Length - 1].ToLower());

                    item.Text = txt + dataReader["StaffID"].ToString().Trim();                    
                    item.Value = txt + dataReader["StaffID"].ToString().Trim();

                    this.writtenBy.Properties.Items.Add(item);
                    this.checkBy.Properties.Items.Add(item);
                }
                dataReader.Close();
                sql_cmd.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        ////////////////////////////////////////////////////
        /// Load Default Data
        ////////////////////////////////////////////////////
        private void LoadDefaultData()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Wiring Daiagram]"
                    + " FROM [231_UnitRR732]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Wiring Daiagram"].ToString().Trim();
                    item.Text = dataReader["Wiring Daiagram"].ToString().Trim();

                    this.wiringDiagram.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo] FROM [231_F5Detail]"
                    + " WHERE ([FinishDate] > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) - 500)";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["SerialNo"].ToString().Trim();
                    item.Text = dataReader["SerialNo"].ToString().Trim();

                    this.PC_Power_Serial.Properties.Items.Add(item);
                    this.Upper_RC420.Properties.Items.Add(item);
                    this.Upper_RD026.Properties.Items.Add(item);
                    this.Lower_RC420.Properties.Items.Add(item);                    
                    this.Lower_RD026.Properties.Items.Add(item);
                    this.A_RC420.Properties.Items.Add(item);                    
                    this.A_RD026.Properties.Items.Add(item);
                    this.Bar_RC420.Properties.Items.Add(item);
                    this.Bar_RD026.Properties.Items.Add(item);
                    this.Z_RC420.Properties.Items.Add(item);
                    this.Z_RD026.Properties.Items.Add(item);
                    this.RC431.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Load Data to Edit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(this.serial))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT t1.*, t2.[CompID]"
                        + " FROM [231_UnitRR732] AS t1 LEFT JOIN"
                        + " [231_RobotOrderDetail] AS t2 ON t1.[SerialNo] COLLATE Vietnamese_CI_AS=t2.[SerialNo]"
                        + " WHERE t1.[SerialNo]='" + this.serial + "'";
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.serialNo.SelectedIndex = this.serialNo.FindString(serial);
                        for (int i = 0; i < this.writtenBy.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.writtenBy.Properties.Items[i];
                            if (item.Value.ToString().ToUpper() == dataReader["Written"].ToString().Trim().ToUpper())
                            {
                                this.writtenBy.SelectedIndex = i;
                            }
                        }                        
                        this.inputDate.EditValue = DateTime.Parse(dataReader["Date"].ToString().Trim());                        
                        for (int i = 0; i < this.checkBy.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.checkBy.Properties.Items[i];
                            if (item.Value.ToString().ToUpper() == dataReader["Checked"].ToString().Trim().ToUpper())
                            {
                                this.checkBy.SelectedIndex = i;
                            }
                        }
                        
                        for (int i = 0; i < this.wiringDiagram.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.wiringDiagram.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Wiring Daiagram"].ToString().Trim())
                            {
                                this.wiringDiagram.SelectedIndex = i;
                            }
                        }
                        this.PC_Software_Ver.Text = dataReader["PC software ver"].ToString().Trim();
                        this.PC_IP.Text = dataReader["PC IP"].ToString().Trim();
                        this.Host_Name.Text = dataReader["Host name"].ToString().Trim();
                        this.RC400_Ver.Text = dataReader["RC400 ver"].ToString().Trim();
                        this.RS485_Ver.Text = dataReader["RS485 ver"].ToString().Trim();
                        this.PC_Power.Text = dataReader["PC Power"].ToString().Trim();
                        this.PC_Power_Serial.Text = dataReader["PC Power Serial"].ToString().Trim();
                        for (int i = 0; i < this.Upper_RC420.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Upper_RC420.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Upper RC420"].ToString().Trim())
                            {
                                this.Upper_RC420.SelectedIndex = i;
                            }
                        }
                        this.Upper_RC420Ver.Text = dataReader["Upper RC420 Ver"].ToString().Trim();
                        this.Upper_RC420ExVer.Text = dataReader["Upper RC420 Ex ver"].ToString().Trim();
                        this.Upper_RC420No.Text = dataReader["Upper RC420 No"].ToString().Trim();

                        for (int i = 0; i < this.Upper_RD026.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Upper_RD026.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Upper RD026"].ToString().Trim())
                            {
                                this.Upper_RD026.SelectedIndex = i;
                            }
                        }
                        this.Upper_Run.Text = dataReader["Upper run"].ToString().Trim();
                        this.Upper_Stop.Text = dataReader["Upper stop"].ToString().Trim();

                        for (int i = 0; i < this.Lower_RC420.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Lower_RC420.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Lower RC420"].ToString().Trim())
                            {
                                this.Lower_RC420.SelectedIndex = i;
                            }
                        }
                        this.Lower_RC420Ver.Text = dataReader["Lower RC420 Ver"].ToString().Trim();
                        this.Lower_RC420ExVer.Text = dataReader["Lower RC420 Ex ver"].ToString().Trim();
                        this.Lower_RC420No.Text = dataReader["Lower RC420 No"].ToString().Trim();

                        for (int i = 0; i < this.Lower_RD026.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Lower_RD026.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Lower RD026"].ToString().Trim())
                            {
                                this.Lower_RD026.SelectedIndex = i;
                            }
                        }
                        this.Lower_Run.Text = dataReader["Lower run"].ToString().Trim();
                        this.Lower_Stop.Text = dataReader["Lower stop"].ToString().Trim();

                        for (int i = 0; i < this.A_RC420.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.A_RC420.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["A RC420"].ToString().Trim())
                            {
                                this.A_RC420.SelectedIndex = i;
                            }
                        }
                        this.A_RC420Ver.Text = dataReader["A RC420 Ver"].ToString().Trim();
                        this.A_RC420ExVer.Text = dataReader["A RC420 Ex ver"].ToString().Trim();
                        this.A_RC420No.Text = dataReader["A RC420 No"].ToString().Trim();

                        for (int i = 0; i < this.A_RD026.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.A_RD026.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["A RD026"].ToString().Trim())
                            {
                                this.A_RD026.SelectedIndex = i;
                            }
                        }
                        this.A_Run.Text = dataReader["A run"].ToString().Trim();
                        this.A_Stop.Text = dataReader["A stop"].ToString().Trim();

                        for (int i = 0; i < this.Bar_RC420.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Bar_RC420.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Bar RC420"].ToString().Trim())
                            {
                                this.Bar_RC420.SelectedIndex = i;
                            }
                        }
                        this.Bar_RC420Ver.Text = dataReader["Bar RC420 Ver"].ToString().Trim();
                        this.Bar_RC420ExVer.Text = dataReader["Bar RC420 Ex ver"].ToString().Trim();
                        this.Bar_RC420No.Text = dataReader["Bar RC420 No"].ToString().Trim();

                        for (int i = 0; i < this.Bar_RD026.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Bar_RD026.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Bar RD026"].ToString().Trim())
                            {
                                this.Bar_RD026.SelectedIndex = i;
                            }
                        }
                        this.Bar_Run.Text = dataReader["Bar run"].ToString().Trim();
                        this.Bar_Stop.Text = dataReader["Bar stop"].ToString().Trim();

                        for (int i = 0; i < this.Z_RC420.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Z_RC420.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Z RC420"].ToString().Trim())
                            {
                                this.Z_RC420.SelectedIndex = i;
                            }
                        }
                        this.Z_RC420Ver.Text = dataReader["Z RC420 Ver"].ToString().Trim();
                        this.Z_RC420ExVer.Text = dataReader["Z RC420 Ex ver"].ToString().Trim();
                        this.Z_RC420No.Text = dataReader["Z RC420 No"].ToString().Trim();

                        for (int i = 0; i < this.Z_RD026.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.Z_RD026.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["Z RD026"].ToString().Trim())
                            {
                                this.Z_RD026.SelectedIndex = i;
                            }
                        }
                        this.Z_Run.Text = dataReader["Z run"].ToString().Trim();
                        this.Z_Stop.Text = dataReader["Z stop"].ToString().Trim();

                        for (int i = 0; i < this.RC431.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.RC431.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["RC431"].ToString().Trim())
                            {
                                this.RC431.SelectedIndex = i;
                            }
                        }
                        this.RC431_ROM_Ver.Text = dataReader["RC431 ROM Ver"].ToString().Trim();
                        this.RC431Ver.Text = dataReader["RC431 Ver"].ToString().Trim();
                        this.RC431No.Text = dataReader["RC431 No"].ToString().Trim();

                        this.Upper_OH.Text = dataReader["Upper-OH"].ToString().Trim();
                        this.Upper_OL.Text = dataReader["Upper-OL"].ToString().Trim();
                        this.Upper_PE.Text = dataReader["Upper-PE"].ToString().Trim();
                        this.Upper_OA.Text = dataReader["Upper-OA"].ToString().Trim();
                        this.Upper_OB.Text = dataReader["Upper-OB"].ToString().Trim();
                        this.Upper_OS.Text = dataReader["Upper-OS"].ToString().Trim();

                        this.Lower_OH.Text = dataReader["Lower-OH"].ToString().Trim();
                        this.Lower_OL.Text = dataReader["Lower-OL"].ToString().Trim();
                        this.Lower_PE.Text = dataReader["Lower-PE"].ToString().Trim();
                        this.Lower_OA.Text = dataReader["Lower-OA"].ToString().Trim();
                        this.Lower_OB.Text = dataReader["Lower-OB"].ToString().Trim();
                        this.Lower_OS.Text = dataReader["Lower-OS"].ToString().Trim();

                        this.A_OH.Text = dataReader["A-OH"].ToString().Trim();
                        this.A_OL.Text = dataReader["A-OL"].ToString().Trim();
                        this.A_PE.Text = dataReader["A-PE"].ToString().Trim();
                        this.A_OA.Text = dataReader["A-OA"].ToString().Trim();
                        this.A_OB.Text = dataReader["A-OB"].ToString().Trim();
                        this.A_OS.Text = dataReader["A-OS"].ToString().Trim();

                        this.B_OH.Text = dataReader["B-OH"].ToString().Trim();
                        this.B_OL.Text = dataReader["B-OL"].ToString().Trim();
                        this.B_PE.Text = dataReader["B-PE"].ToString().Trim();
                        this.B_OA.Text = dataReader["B-OA"].ToString().Trim();
                        this.B_OB.Text = dataReader["B-OB"].ToString().Trim();
                        this.B_OS.Text = dataReader["B-OS"].ToString().Trim();

                        this.Z_OH.Text = dataReader["Z-OH"].ToString().Trim();
                        this.Z_OL.Text = dataReader["Z-OL"].ToString().Trim();
                        this.Z_PE.Text = dataReader["Z-PE"].ToString().Trim();
                        this.Z_OA.Text = dataReader["Z-OA"].ToString().Trim();
                        this.Z_OB.Text = dataReader["Z-OB"].ToString().Trim();
                        this.Z_OS.Text = dataReader["Z-OS"].ToString().Trim();

                        this.Inter_OH.Text = dataReader["Inter-OH"].ToString().Trim();
                        this.Inter_OL.Text = dataReader["Inter-OL"].ToString().Trim();
                        this.Inter_OA.Text = dataReader["Inter-OA"].ToString().Trim();
                        this.Inter_OB.Text = dataReader["Inter-OB"].ToString().Trim();
                        this.Inter_OS.Text = dataReader["Inter-OS"].ToString().Trim();

                        this.Remark.Text = dataReader["Remark"].ToString().Trim();
                    }
                    sql_cmd.Dispose();
                    myConnection.Close();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                    ret = false;
                }
            }
            return ret;
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;

            foreach (Control c in this.Controls)
            {
                if (c is System.Windows.Forms.ComboBox)
                {
                    if (((System.Windows.Forms.ComboBox)c).SelectedIndex < 0)
                    {
                        if (required_list.ContainsKey(((System.Windows.Forms.ComboBox)c).Name))
                        {
                            error = "Please select " + required_list[((System.Windows.Forms.ComboBox)c).Name];
                        }
                    }
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    if (((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex < 0)
                    {
                        if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)c).Name))
                        {
                            error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)c).Name];
                        }
                    }
                }

                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)c).Text.Trim()))
                    {
                        if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)c).Name))
                        {
                            error = "Please enter " + required_list[((DevExpress.XtraEditors.TextEdit)c).Name];
                        }
                    }
                }

                if (c is System.Windows.Forms.TextBox)
                {
                    if (string.IsNullOrEmpty(((System.Windows.Forms.TextBox)c).Text.Trim()))
                    {
                        if (required_list.ContainsKey(((System.Windows.Forms.TextBox)c).Name))
                        {
                            error = "Please enter " + required_list[((System.Windows.Forms.TextBox)c).Name];
                        }
                    }
                }                
            }            

            if (string.IsNullOrEmpty(error))
            {
                DateTime date = DateTime.Parse(this.inputDate.EditValue.ToString());
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "UPDATE [231_UnitRR732] SET"
                        + " [SerialNo]='" + this.serialNo.Text.Trim() + "', [Written]='" + this.writtenBy.Text.Trim() + "',"
                        + " [Date]=@date, [Checked]='" + this.checkBy.Text.Trim() + "',"
                        + " [Wiring Daiagram]='" + this.wiringDiagram.Text.Trim() + "',"
                        + " [PC software ver]=NULLIF('" + this.PC_Software_Ver.Text.Trim() + "', ''),"
                        + " [PC IP]=NULLIF('" + this.PC_IP.Text.Trim() + "', ''),"
                        + " [Host name]=NULLIF('" + this.Host_Name.Text.Trim() + "', ''),"
                        + " [RC400 ver]=NULLIF('" + this.RC400_Ver.Text.Trim() + "', ''),"
                        + " [RS485 ver]=NULLIF('" + this.RS485_Ver.Text.Trim() + "', ''),"
                        + " [PC Power]=NULLIF('" + this.PC_Power.Text.Trim() + "', ''),"
                        + " [PC Power Serial]='" + this.PC_Power_Serial.Text.Trim() + "',"

                        + " [Upper RC420]='" + this.Upper_RC420.Text.Trim() + "',"
                        + " [Upper RC420 Ver]='" + this.Upper_RC420Ver.Text.Trim() + "',"
                        + " [Upper RC420 Ex ver]=NULLIF('" + this.Upper_RC420ExVer.Text.Trim() + "', ''),"
                        + " [Upper RC420 No]='" + this.Upper_RC420No.Text.Trim() + "',"
                        + " [Upper RD026]='" + this.Upper_RD026.Text.Trim() + "',"
                        + " [Upper run]='" + this.Upper_Run.Text.Trim() + "',"
                        + " [Upper stop]='" + this.Upper_Stop.Text.Trim() + "',"

                        + " [Lower RC420]='" + this.Lower_RC420.Text.Trim() + "',"
                        + " [Lower RC420 Ver]='" + this.Lower_RC420Ver.Text.Trim() + "',"
                        + " [Lower RC420 Ex ver]=NULLIF('" + this.Lower_RC420ExVer.Text.Trim() + "', ''),"
                        + " [Lower RC420 No]='" + this.Lower_RC420No.Text.Trim() + "',"
                        + " [Lower RD026]='" + this.Lower_RD026.Text.Trim() + "',"
                        + " [Lower run]='" + this.Lower_Run.Text.Trim() + "',"
                        + " [Lower stop]='" + this.Lower_Stop.Text.Trim() + "',"

                        + " [A RC420]='" + this.A_RC420.Text.Trim() + "',"
                        + " [A RC420 Ver]='" + this.A_RC420Ver.Text.Trim() + "',"
                        + " [A RC420 Ex ver]=NULLIF('" + this.A_RC420ExVer.Text.Trim() + "', ''),"
                        + " [A RC420 No]='" + this.A_RC420No.Text.Trim() + "',"
                        + " [A RD026]='" + this.A_RD026.Text.Trim() + "',"
                        + " [A run]='" + this.A_Run.Text.Trim() + "',"
                        + " [A stop]='" + this.A_Stop.Text.Trim() + "',"

                        + " [Bar RC420]='" + this.Bar_RC420.Text.Trim() + "',"
                        + " [Bar RC420 Ver]='" + this.Bar_RC420Ver.Text.Trim() + "',"
                        + " [Bar RC420 Ex ver]=NULLIF('" + this.Bar_RC420ExVer.Text.Trim() + "', ''),"
                        + " [Bar RC420 No]='" + this.Bar_RC420No.Text.Trim() + "',"
                        + " [Bar RD026]='" + this.Bar_RD026.Text.Trim() + "',"
                        + " [Bar run]='" + this.Bar_Run.Text.Trim() + "',"
                        + " [Bar stop]='" + this.Bar_Stop.Text.Trim() + "',"

                        + " [Z RC420]=NULLIF('" + this.Z_RC420.Text.Trim() + "', ''),"
                        + " [Z RC420 Ver]=NULLIF('" + this.Z_RC420Ver.Text.Trim() + "', ''),"
                        + " [Z RC420 Ex ver]=NULLIF('" + this.Z_RC420ExVer.Text.Trim() + "', ''),"
                        + " [Z RC420 No]=NULLIF('" + this.Z_RC420No.Text.Trim() + "', ''),"
                        + " [Z RD026]=NULLIF('" + this.Z_RD026.Text.Trim() + "', ''),"
                        + " [Z run]=NULLIF('" + this.Z_Run.Text.Trim() + "', ''),"
                        + " [Z stop]=NULLIF('" + this.Z_Stop.Text.Trim() + "', ''),"

                        + " [RC431]='" + this.RC431.Text.Trim() + "',"
                        + " [RC431 Ver]='" + this.RC431Ver.Text.Trim() + "',"
                        + " [RC431 ROM ver]='" + this.RC431_ROM_Ver.Text.Trim() + "',"
                        + " [RC431 No]='" + this.RC431No.Text.Trim() + "',"

                        + " [Upper-OH]=NULLIF('" + this.Upper_OH.Text.Trim() + "', ''), [Upper-OL]=NULLIF('" + this.Upper_OL.Text.Trim() + "', ''),"
                        + " [Upper-PE]=NULLIF('" + this.Upper_PE.Text.Trim() + "', ''), [Upper-OA]=NULLIF('" + this.Upper_OA.Text.Trim() + "', ''),"
                        + " [Upper-OB]=NULLIF('" + this.Upper_OB.Text.Trim() + "', ''), [Upper-OS]=NULLIF('" + this.Upper_OS.Text.Trim() + "', ''),"

                        + " [Lower-OH]=NULLIF('" + this.Lower_OH.Text.Trim() + "', ''), [Lower-OL]=NULLIF('" + this.Lower_OL.Text.Trim() + "', ''),"
                        + " [Lower-PE]=NULLIF('" + this.Lower_PE.Text.Trim() + "', ''), [Lower-OA]=NULLIF('" + this.Lower_OA.Text.Trim() + "', ''),"
                        + " [Lower-OB]=NULLIF('" + this.Lower_OB.Text.Trim() + "', ''), [Lower-OS]=NULLIF('" + this.Lower_OS.Text.Trim() + "', ''),"

                        + " [A-OH]=NULLIF('" + this.A_OH.Text.Trim() + "', ''), [A-OL]=NULLIF('" + this.A_OL.Text.Trim() + "', ''),"
                        + " [A-PE]=NULLIF('" + this.A_PE.Text.Trim() + "', ''), [A-OA]=NULLIF('" + this.A_OA.Text.Trim() + "', ''),"
                        + " [A-OB]=NULLIF('" + this.A_OB.Text.Trim() + "', ''), [A-OS]=NULLIF('" + this.A_OS.Text.Trim() + "', ''),"

                        + " [B-OH]=NULLIF('" + this.B_OH.Text.Trim() + "', ''), [B-OL]=NULLIF('" + this.B_OL.Text.Trim() + "', ''),"
                        + " [B-PE]=NULLIF('" + this.B_PE.Text.Trim() + "', ''), [B-OA]=NULLIF('" + this.B_OA.Text.Trim() + "', ''),"
                        + " [B-OB]=NULLIF('" + this.B_OB.Text.Trim() + "', ''), [B-OS]=NULLIF('" + this.B_OS.Text.Trim() + "', ''),"

                        + " [Z-OH]=NULLIF('" + this.Z_OH.Text.Trim() + "', ''), [Z-OL]=NULLIF('" + this.Z_OL.Text.Trim() + "', ''),"
                        + " [Z-PE]=NULLIF('" + this.Z_PE.Text.Trim() + "', ''), [Z-OA]=NULLIF('" + this.Z_OA.Text.Trim() + "', ''),"
                        + " [Z-OB]=NULLIF('" + this.Z_OB.Text.Trim() + "', ''), [Z-OS]=NULLIF('" + this.Z_OS.Text.Trim() + "', ''),"
                        + " [Z-0S]=NULLIF('" + this.Z_0S.Text.Trim() + "', ''),"

                        + " [Inter-OH]=NULLIF('" + this.Inter_OH.Text.Trim() + "', ''), [Inter-OL]=NULLIF('" + this.Inter_OL.Text.Trim() + "', ''),"
                        + " [Inter-OA]=NULLIF('" + this.Inter_OA.Text.Trim() + "', ''),"
                        + " [Inter-OB]=NULLIF('" + this.Inter_OB.Text.Trim() + "', ''), [Inter-OS]=NULLIF('" + this.Inter_OS.Text.Trim() + "', ''),"

                        + " [Remark]='" + this.Remark.Text.Trim() + "'"
                        + " WHERE [SerialNo]='" + this.serialNo.Text.Trim() + "'";
                    sql_cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = date.Date;
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Database updated successfully!");
                    this.Dispose();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                }                
            }
            else
            {
                MessageBox.Show(error);
            }
        }
      
    }
}
